package com.hualan.sfzy.controller;

import com.hualan.sfzy.entity.Cspi;
import com.hualan.sfzy.service.CspiService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/cspi")
public class CspiController {
    @Autowired
    private CspiService cspiService;

    @GetMapping
    public Result fineById(Integer id) {
        Cspi cspi = cspiService.findById(id);
        if (cspi == null) {
            return Result.error("查询成功");
        }
        return Result.success(cspi);
    }

    @GetMapping("/all")
    public Result findAll(){
        List<Cspi> list = cspiService.findCspiAndCounselorAll();
        if(list.isEmpty()){
            return Result.error("查找失败或数据库中没有数据");
        }
        return Result.success(list);
    }

    @GetMapping("/page")
    public Result findPage(Cspi cspi, Integer pageNum, Integer pageSize){
        Map<String,Object> map = cspiService.findPage(pageNum,pageSize,cspi);
        return Result.success(map);
    }

    @DeleteMapping("/batch")
    public Result removeBatch(@RequestParam Integer[] ids){
        int count = cspiService.removeBatch(ids);
        if(count > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @DeleteMapping()
    public Result remove(@RequestParam Integer id){
        int count = cspiService.remove(id);
        if(count > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PostMapping("/add-selective")
    public Result addSelective(@RequestBody Cspi cspi){
        int count = cspiService.addSelective(cspi);
        if(count > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping
    public Result editSelective(@RequestBody Cspi cspi){
        int count = cspiService.edit(cspi);
        if(count > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

}
